﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Data.Json;
using XiaoweiV2;

namespace XiaoweiVoiceAI.ResponseCreators
{
    internal static class AlarmResponseCreator
    {
        private class ClockData : IClockData
        {
            public long Clock_id { get; set; }

            public string Clock_event { get; set; }

            public string Repeat_interval { get; set; }

            public ClockRepeatType Repeat_type { get; set; }

            public int Service_type { get; set; }

            public long Trig_time { get; set; }

            public ClockOpt ClockOpt { get; set; }
        }
        private class AlarmResponseData : IAlarmResponseData
        {
            public IReadOnlyList<IClockData> Clock_info { get; set; }

            public VoiceAI.ResponseType ResponseType => VoiceAI.ResponseType.Alarm;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }
            public string ResponseText { get; set; }
            public string TTSUrl { get; set; }

            public bool RestoreInHistory { get; set; }

            public object DataContext { get; set; }
        }
        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, ResponseParam param)
        {
            if (String.IsNullOrEmpty(param.ResponseData)) return null;
            JsonObject jsonObject = JsonObject.Parse(param.ResponseData);
            List<IClockData> Clock_info = new List<IClockData>();
            foreach (var item in jsonObject.GetNamedArray("clock_info"))
            {
                if (item.ValueType == JsonValueType.Object)
                {
                    JsonObject jsonObject2 = item.GetObject();

                    ClockData clockData = new ClockData()
                    {
                        Clock_id = (long)jsonObject2.GetNamedNumber("clock_id"),
                        Clock_event = jsonObject2.GetNamedString("event"),
                        Repeat_interval = jsonObject2.GetNamedString("repeat_interval"),
                        Repeat_type = (ClockRepeatType)jsonObject2.GetNamedNumber("repeat_type"),
                        Service_type = (int)jsonObject2.GetNamedNumber("service_type"),
                        Trig_time = (long)jsonObject2.GetNamedNumber("trig_time"),
                        ClockOpt = (ClockOpt)jsonObject2.GetNamedNumber("opt", 1)
                    };
                    Clock_info.Add(clockData);
                }
            }
            AlarmResponseData alarmResponseData = new AlarmResponseData();
            alarmResponseData.RequestText = param.RequestText;
            alarmResponseData.ResponseText = param.ResponseText;
            alarmResponseData.TTSUrl = XiaoweiV2.ResponseHelper.GetTTSUrl(param);
            alarmResponseData.Clock_info = Clock_info;
            alarmResponseData.RestoreInHistory = !string.IsNullOrEmpty(param.RequestText);
            return alarmResponseData;
        }
    }
}
